// 给你一个链表的头节点 head 和一个整数 val ，请你删除链表中所有满足 Node.val == val 的节点，并返回 新的头节点 。

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
function ListNode(val, next) {
    this.val = (val === undefined ? 0 : val)
    this.next = (next === undefined ? null : next)
}
/**
 * @param {ListNode} head
 * @param {number} val
 * @return {ListNode}
 */
var removeElements = function (head, val) {
    if (!head) return head;
    let pre = head;
    let next = head.next;

    while (next !== null) {
        if (next.val === val) {
            const temp = next.next;
            next = next.next;
            pre.next = temp;
        } else {
            next = next.next;
            pre = pre.next;
        }
    }

    if (next !== null && next.val === val) head = null;
    // console.log(JSON.stringify(head))
    return head;
};

module.exports = removeElements;